package com.xhx.accounting.ui.home;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.RecyclerView;

import com.xhx.accounting.R;
import com.xhx.accounting.dayDetailActivity;

import org.w3c.dom.Text;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.ViewHolder> {
    private List<Map<String, Object>> mDataSet;
    private Context mContext;

    public static class ViewHolder extends RecyclerView.ViewHolder {
        private final TextView date;
        private final TextView money;
        private final LinearLayout detailLV;
        private final ConstraintLayout rootView;

        public ViewHolder(View v) {
            super(v);
            date = v.findViewById(R.id.date);
            money = v.findViewById(R.id.money);
            detailLV = v.findViewById(R.id.detailLV);
            rootView = v.findViewById(R.id.root_BG);
        }

        public TextView getDate() {
            return date;
        }

        public TextView getMoney() {
            return money;
        }
        public LinearLayout getDetailLV() { return detailLV; }
        public ConstraintLayout getRootView() {return rootView;}
    }

    public CustomAdapter(List<Map<String, Object>> dataSet) {
        mDataSet = dataSet;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        // Create a new view.
        mContext = viewGroup.getContext();
        View v = LayoutInflater.from(mContext)
                .inflate(R.layout.day_list_item, viewGroup, false);

        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, final int position) {
        long timeStamp = (long) mDataSet.get(position).get("timeStamp");
        Date date = new Date(timeStamp*1000);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String date1 = sdf.format(date);
        viewHolder.getDate().setText(date1);
        viewHolder.getMoney().setText("支出" + mDataSet.get(position).get("cost") + "  收入" + mDataSet.get(position).get("income"));
        //二级列表
        LinearLayout v = viewHolder.getDetailLV();
        v.removeAllViews();
        List list = (List)mDataSet.get(position).get("recordAll");
        int count = list.size();
        for (int i = 0; i < count; i++) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.dailydetail, null);
            TextView name = view.findViewById(R.id.detailName);
            TextView amount = view.findViewById(R.id.detailAmount);
            ImageView icon = view.findViewById(R.id.detailIcon);
            name.setText((String)((Map)list.get(i)).get("typeName"));
            //金额计算
            String sign;
            if ((int)((Map)list.get(i)).get("classification_direction") == -1) {
                sign = "-";
            } else {
                sign = "+";
            }
            amount.setText(sign + ((Map)list.get(i)).get("amount"));
            icon.setImageResource(mContext.getResources().getIdentifier((String)((Map)list.get(i)).get("typeIcon"),"drawable",mContext.getPackageName()));
            v.addView(view);
        }
        viewHolder.getRootView().setOnClickListener(v1 -> {
            Toast.makeText(mContext, position + "", Toast.LENGTH_LONG).show();
            Log.d("timeStamp", String.valueOf(timeStamp));
            Intent intent = new Intent();
            intent.setClass(mContext, dayDetailActivity.class);
            intent.putExtra("timeStamp", timeStamp);
            mContext.startActivity(intent);
        });

    }

    @Override
    public int getItemCount() {
        return mDataSet.size();
    }

}